cd /Users/Laura/Documents/Research/PDHosp/Replication
log using WMKM_REStat_nhis_regs.log, replace
/************************************************************
PROGRAM: WMKM_REStat_nhis_regs.do
PURPOSE: RD analysis of NHIS 1992-1996 files for 
		 Wherry, Miller, Kaestner, and Meyer
DATE: 	 January 30, 2017
NOTES:	 To run, (1) change directory above to a local directory. 
		 (2) Create subfolders called src, data, and output. 
		 (3) Put source NHIS data files in src directory. 
CONTACT: Laura Wherry, lwherry@mednet.ucla.edu
*************************************************************/
set more off

/*************************************************************
A) PREPARE NHIS DATA FILES FOR ANALYSIS
*read in NHIS health insurance supplements for 1992 - 1996
*roughly corresponds to ages 8 - 12 for Oct 1983 cohort
**downloaded from: ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/NHIS/
*************************************************************/
forval y=1992/1996{
	use src/healthin`y', clear

	if `y'==1992{
		egen nhispnid=concat(year quarter psunumr weekcen segnum hhnum pnum)
	}
	
	*recode health insurance vars, note: slightly different in 1992
	*note: to be consistent across years, coverage vars
	*take on a value of zero if no coverage or coverage status unknown
	if `y'==1992{
		gen coverage=1*(phlthinr==1)
		replace coverage=1 if hlthinsr==1
		replace coverage=1 if inrange(pubassr,1,2)
		recode medicaid (2=0) (7/9=.)
	}
	if inrange(`y',1993,1996){
		recode covstatr (2/3=0), gen(coverage)
		if inrange(`y',1993,1994){
			recode maidlmo (2=0) (7/9=.), gen(medicaid)
		}
		if inrange(`y',1995,1996){
			recode maidlamo (2=0) (7/9=.), gen(medicaid)
		}
	}
	label var coverage "Health insurance coverage status"
	label var medicaid "Medicaid coverage"
	
	*recode health care utilization vars
	recode dv12 (1/997=1) (998=.), gen(doc12)
	label var doc12 "Doctor visit in the last 12 mos"
	gen anyhosp12=1*(hep12x>0)
	label var anyhosp12 "Any short stay hosp ep in past 12 mos (excluding delivery)"
	
	*recode poverty variable
	recode poverty (1=0) (2=1) (3=.)
	
	*restrict sample to those with birth month and birth year info
	destring birthmo birthyr, replace
	drop if birthmo==99 | birthyr==9999
	
	if `y'==1993{
		rename wtfs wtfa
	}
	
	*keep variables need for analyis
	if `y'==1992{
		keep nhispnid age racer3 poverty wtfa birthmo birthyr coverage medicaid doc12 anyhosp12 
	}
	if `y'!=1992{
		keep age racer3 poverty wtfa birthmo birthyr coverage medicaid doc12 anyhosp12
	}
	gen svyyr=`y'
	su
	save data/nhishi`y', replace
}

*for 1992 year, drop Hispanic oversample
use src/personsnh1992, clear
egen nhispnid=concat(year quarter psunumr weekcen segnum hhnum pnum)
gen flag=1
keep nhispnid flag
merge 1:1 nhispnid using data/nhishi1992, keep(match) nogen
keep if flag==1
save data/nhishi1992, replace

*append all years of data
use data/nhishi1992, clear
append using data/nhishi1993 data/nhishi1994 data/nhishi1995 data/nhishi1996 
!rm data/nhishi*

*restrict to cohorts born between Sept 1979 - October 1987
keep if inrange(birthyr,1979,1987)
drop if birthyr==1979 & inrange(birthmo,1,9)
drop if birthyr==1987 & inrange(birthmo,10,12)
saveold data/nhisallnh, replace

/*************************************************************
B) RD ANALYSIS OF HEALTH INSURANCE COVERAGE AND UTILIZATION
*local linear regression implemented with rdrobust command
*downloaded here: https://sites.google.com/site/rdpackages/rdrobust
*************************************************************/
use data/nhisallnh, clear

*create regression vars
sort birthyr birthmo
egen num=group(birthyr birthmo)
gen c=num-49
gen post=c>=0
gen c2=c^2
gen postc=c*post
gen postc2=c2*post

gen win3=1*(inrange(c,-36,35))
gen win2=1*(inrange(c,-24,23))

*baseline means
mean coverage medicaid doc12 anyhosp12 if c<0 
mean coverage medicaid doc12 anyhosp12 if c<0 & racer3==1
mean coverage medicaid doc12 anyhosp12 if c<0 & racer3==2
mean coverage medicaid doc12 anyhosp12 if c<0 & poverty==1
mean coverage medicaid doc12 anyhosp12 if c<0 & poverty==0
mean medicaid coverage if c<0 & poverty==1 & racer3==1
mean medicaid coverage if c<0 & poverty==1 & racer3==2

capture program drop spec
program define spec

di "ALL RACES, `1'"
regress `1' post c c2 postc postc2 i.birthmo, cluster(c)
outreg2 using output/`1', replace stats(coef ci) excel bdec(3) 
regress `1' post c c2 postc postc2 i.birthmo if win3, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3) 
regress `1' post c c2 postc postc2 i.birthmo if win2, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
qui: regress `1' i.birthmo, robust 
predict ypub, xb
gen epub=`1'-ypub		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)

drop ypub
drop epub 

di "BLACKS, `1'"
regress `1' post c c2 postc postc2 i.birthmo if racer3==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & racer3==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & racer3==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
qui: regress `1' i.birthmo if racer3==1, robust 
predict ypub if racer3==1, xb
gen epub=`1'-ypub if racer3==1
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)
drop ypub
drop epub 

di "NON-BLACKS, `1'"
regress `1' post c c2 postc postc2 i.birthmo if racer3==2, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & racer3==2, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & racer3==2, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
qui: regress `1' i.birthmo if racer3==2, robust 
predict ypub if racer3==2, xb
gen epub=`1'-ypub if racer3==2
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)
drop ypub
drop epub 

di "POVERTY, `1'"
regress `1' post c c2 postc postc2 i.birthmo if poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)

qui: regress `1' i.birthmo if poverty==1, robust 
predict ypub if poverty==1, xb
gen epub=`1'-ypub if poverty==1
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)

drop ypub
drop epub 

di "NON-POVERTY, `1'"
regress `1' post c c2 postc postc2 i.birthmo if poverty==0, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & poverty==0, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & poverty==0, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)

qui: regress `1' i.birthmo if poverty==0, robust 
predict ypub if poverty==0, xb
gen epub=`1'-ypub if poverty==0
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)

drop ypub
drop epub

di "BLACKS x POVERTY, `1'"
regress `1' post c c2 postc postc2 i.birthmo if racer3==1 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & racer3==1 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & racer3==1 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
qui: regress `1' i.birthmo if racer3==1, robust 
predict ypub if racer3==1 & poverty==1, xb
gen epub=`1'-ypub if racer3==1 & poverty==1
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)
drop ypub
drop epub 

di "NON-BLACKS x POVERTY, `1'"
regress `1' post c c2 postc postc2 i.birthmo if racer3==2 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win3 & racer3==2 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
regress `1' post c c2 postc postc2 i.birthmo if win2 & racer3==2 & poverty==1, cluster(c)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
qui: regress `1' i.birthmo if racer3==2, robust 
predict ypub if racer3==2 & poverty==1, xb
gen epub=`1'-ypub if racer3==2 & poverty==1
		
rdrobust epub c, bwselect(IK)
outreg2 using output/`1', stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/`1', stats(coef ci) excel bdec(3)
drop ypub
drop epub 
 
end

***medicaid coverage in last month
spec medicaid

***any coverage in last month
spec coverage

***doctor's visit in last 12 months
spec doc12

***any short-stay hospital stay in last 12 months (excl. delivery)
spec anyhosp12

*output data to construct graphs
outsheet using data/nhis.csv, c replace

log close



